﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Windows.Controls.Ribbon;

namespace MapEditor
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : RibbonWindow
    {
        // chuyen doi 1px = 0.75*Point || double * 0.75 = 1px;
        int ScaleBackGround = 1;
        string m_TextImage;
        TileMap m_TileMap = null;

        string m_Information = "Id\tIndex\tX\tY\tWidth\tHeight" + Environment.NewLine + "Id = 0: Tile Map" + Environment.NewLine;
        List<Node> m_ListNode = new List<Node>();
        List<Node> m_ListNodeImage = new List<Node>();
        List<Rectangle> m_ListRectangle = new List<Rectangle>();
        Rectangle m_Rect = null;
        private Point m_CurrentPoint;
        private Point m_LastPoint;
        bool isMouseMove_Pressed = false;

        public MainWindow()
        {
            InitializeComponent();
            select_Image.Source = null;
        }


        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog openfile = new Microsoft.Win32.OpenFileDialog();

            openfile.Filter = "All File | *.png; *.jpg; *.jpeg; *.gif";

            Nullable<bool> result = openfile.ShowDialog();

            if (result == true)
            {
                m_TextImage = openfile.FileName;
            }
            else return;

            ImageBrush img = new ImageBrush();
            img.ImageSource = new BitmapImage(new Uri(m_TextImage));

            mycanvas.Width = img.ImageSource.Width * ScaleBackGround;
            mycanvas.Height = img.ImageSource.Height * ScaleBackGround;
            mycanvas.Background = img;


            gridView.Width = mycanvas.Width;
            gridView.Height = mycanvas.Height;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            int x = Convert.ToInt16(TextX.Text.ToString());
            int y = Convert.ToInt16(TextY.Text.ToString());

            for (int i = 0; i < m_ListNodeImage.Count; i++)
                m_ListNode.Add(m_ListNodeImage[i]);

            m_TileMap = new TileMap(m_TextImage, m_ListNode, m_Information, x, y);
            m_TileMap.CreateRectBit();
            m_TileMap.CreateTileMap();

            m_TileMap.SaveFileTileMap();
        }

        private void mycanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            m_LastPoint = e.GetPosition(mycanvas);

            m_Rect = new Rectangle();
            m_Rect.Width = 0;
            m_Rect.Height = 0;
            m_Rect.Stroke = Brushes.Black;
            Canvas.SetLeft(m_Rect, m_LastPoint.X);
            Canvas.SetTop(m_Rect, m_LastPoint.Y);

            mycanvas.Children.Add(m_Rect);
        }

        private void mycanvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                mycanvas.Children.Remove(m_Rect);

                m_CurrentPoint = e.GetPosition(mycanvas);

                double width = (int)Math.Abs(m_CurrentPoint.X - m_LastPoint.X);
                double height = (int)Math.Abs(m_CurrentPoint.Y - m_LastPoint.Y);

                m_Rect.Width = width;
                m_Rect.Height = height;
                m_Rect.Stroke = Brushes.Black;

                if (m_LastPoint.X > m_CurrentPoint.X)
                    Canvas.SetLeft(m_Rect, (int)m_CurrentPoint.X);
                else
                    Canvas.SetLeft(m_Rect, (int)m_LastPoint.X);


                if (m_LastPoint.Y < m_CurrentPoint.Y)
                    Canvas.SetTop(m_Rect, (int)m_LastPoint.Y);
                else
                    Canvas.SetTop(m_Rect, (int)m_CurrentPoint.Y);

                mycanvas.Children.Add(m_Rect);

                if (width != 0 || height != 0)
                    isMouseMove_Pressed = true;
            }
        }

        private void mycanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if(select_Image.Source != null)
            {
                Point point = e.GetPosition(mycanvas);
                point.X -= (point.X % (Convert.ToInt16(TextX.Text.ToString()) / 0.75)) ;
                point.Y -= (point.Y % (Convert.ToInt16(TextY.Text.ToString()) / 0.75));

                Image img = new Image();
                img.Source = select_Image.Source;

                img.Width = Convert.ToInt16(TextX.Text) / 0.75; 
                img.Height = Convert.ToInt16(TextY.Text) / 0.75;
                mycanvas.Children.Add(img);
                Canvas.SetLeft(img, point.X);
                Canvas.SetTop(img, point.Y);


                bool Exist = false;
                for (int i = 0; i < m_ListNodeImage.Count; i++)
                {
                    if (m_ListNodeImage[i].m_X == (int)(0.75 * point.X)
                        && m_ListNodeImage[i].m_Y == (int)((point.Y) * 0.75))
                    {
                        m_ListNodeImage[i].m_Id = Convert.ToInt16(InputID.Text.ToString());
                        Exist = true;
                        break;
                    }
                }


                MessageBox.Show((int)(0.75 * point.X) + ", " + (int)((point.Y) * 0.75));
                if(!Exist)
                     m_ListNodeImage.Add(new Node(Convert.ToInt16(InputID.Text.ToString()), 0, (int)(0.75 * point.X), (int)(( point.Y) * 0.75), Convert.ToInt16(TextX.Text.ToString()), Convert.ToInt16(TextY.Text.ToString())));
            }
            else if (isMouseMove_Pressed)
            {
                m_ListRectangle.Add(m_Rect);

                Node node = new Node(Convert.ToInt16(InputID.Text.ToString()), 0, 0, 0);

                if (m_LastPoint.X < m_CurrentPoint.X)
                    node.m_X = (int)(m_LastPoint.X * 0.75 / ScaleBackGround);
                else
                    node.m_X = (int)(m_CurrentPoint.X * 0.75 / ScaleBackGround);

                if (m_LastPoint.Y < m_CurrentPoint.Y)
                    node.m_Y = (int)(m_LastPoint.Y * 0.75 / ScaleBackGround);
                else
                    node.m_Y = (int)(m_CurrentPoint.Y * 0.75 / ScaleBackGround);

                node.m_Width = (int)Math.Abs((m_LastPoint.X - m_CurrentPoint.X) * 0.75 / ScaleBackGround);
                node.m_Height = (int)Math.Abs((m_LastPoint.Y - m_CurrentPoint.Y) * 0.75 / ScaleBackGround);

                m_ListNode.Add(node);
            }

            isMouseMove_Pressed = false;
        }

        // xoa rectangle da ve tai vi tri chuot
        private void mycanvas_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            Point temp = e.GetPosition(mycanvas);

            for (int i = 0; i < m_ListNode.Count; i++)
            {
                if (m_ListNode[i].m_X < temp.X && temp.X < m_ListNode[i].m_X + m_ListNode[i].m_Width
                    && m_ListNode[i].m_Y < temp.Y && temp.Y < m_ListNode[i].m_Y + m_ListNode[i].m_Height)
                {
                    m_Rect = m_ListRectangle[i];
                    mycanvas.Children.Remove(m_Rect);
                    break;
                }
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            m_Information += "ID = " + InputID.Text.ToString() + ": " + InputInfor.Text.ToString() + Environment.NewLine;
            MessageBox.Show("Saved");
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            double x = Convert.ToDouble(TextX.Text.ToString()) * ScaleBackGround;
            double y = Convert.ToDouble(TextY.Text.ToString()) * ScaleBackGround;

            for (int i = 0; i < mycanvas.Width * 0.75 / x; i++)
            {
                Line line = new Line();
                line.Stroke = System.Windows.Media.Brushes.LightSteelBlue;

                line.X1 = (double)i * x / 0.75;
                line.Y1 = 0;
                line.X2 = line.X1;
                line.Y2 = mycanvas.Height;

                mycanvas.Children.Add(line);
            }

            for (int i = 0; i < mycanvas.Height * 0.75 / y; i++)
            {
                Line line = new Line();
                line.Stroke = System.Windows.Media.Brushes.LightSteelBlue;

                line.X1 = 0;
                line.Y1 = (double)i * y / 0.75;
                line.X2 = mycanvas.Width;
                line.Y2 = line.Y1;

                mycanvas.Children.Add(line);
            }
        }

        private void i10_Click(object sender, RoutedEventArgs e)
        {
            select_Image.Source = i10.LargeImageSource;
            InputID.Text = i10.Label;
            InputInfor.Text = "Block item";
        }

        private void i11_Click(object sender, RoutedEventArgs e)
        {
            select_Image.Source = i11.LargeImageSource;
            InputID.Text = i11.Label;
            InputInfor.Text = "Tank";
        }

        private void i12_Click(object sender, RoutedEventArgs e)
        {
            select_Image.Source = i12.LargeImageSource;
            InputID.Text = i12.Label;
            InputInfor.Text = "Block Tank";
        }

        private void i13_Click(object sender, RoutedEventArgs e)
        {
            select_Image.Source = i13.LargeImageSource;
            InputID.Text = i13.Label;
            InputInfor.Text = "SoldierSniper";
        }

        private void i16_Click(object sender, RoutedEventArgs e)
        {
            select_Image.Source = i16.LargeImageSource;
            InputID.Text = i16.Label;
            InputInfor.Text = "Soldier";
        }
    }
}
